home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / g++-dist / gcc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-20  |  52.9 KB  |  2,140 lines

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987,1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. This paragraph is here to try to keep Sun CC from dying.
  21. The number of chars here seems crucial!!!!  */
  22.  
  23. void record_temp_file ();
  24.  
  25. /* This program is the user interface to the C compiler and possibly to
  26. other compilers.  It is used because compilation is a complicated procedure
  27. which involves running several programs and passing temporary files between
  28. them, forwarding the users switches to those programs selectively,
  29. and deleting the temporary files at the end.
  30.  
  31. CC recognizes how to compile each input file by suffixes in the file names.
  32. Once it knows which kind of compilation to perform, the procedure for
  33. compilation is specified by a string called a "spec".
  34.  
  35. Specs are strings containing lines, each of which (if not blank)
  36. is made up of a program name, and arguments separated by spaces.
  37. The program name must be exact and start from root, since no path
  38. is searched and it is unreliable to depend on the current working directory.
  39. Redirection of input or output is not supported; the subprograms must
  40. accept filenames saying what files to read and write.
  41.  
  42. In addition, the specs can contain %-sequences to substitute variable text
  43. or for conditional text.  Here is a table of all defined %-sequences.
  44. Note that spaces are not generated automatically around the results of
  45. expanding these sequences; therefore, you can concatenate them together
  46. or with constant text in a single argument.
  47.  
  48.  %%    substitute one % into the program name or argument.
  49.  %i     substitute the name of the input file being processed.
  50.  %b     substitute the basename of the input file being processed.
  51.     This is the substring up to (and not including) the last period.
  52.  %g     substitute the temporary-file-name-base.  This is a string chosen
  53.     once per compilation.  Different temporary file names are made by
  54.     concatenation of constant strings on the end, as in `%g.s'.
  55.     %g also has the same effect of %d.
  56.  %d    marks the argument containing or following the %d as a
  57.     temporary file name, so that that file will be deleted if CC exits
  58.     successfully.  Unlike %g, this contributes no text to the argument.
  59.  %w    marks the argument containing or following the %w as the
  60.     "output file" of this compilation.  This puts the argument
  61.     into the sequence of arguments that %o will substitute later.
  62.  %o    substitutes the names of all the output files, with spaces
  63.     automatically placed around them.  You should write spaces
  64.     around the %o as well or the results are undefined.
  65.     %o is for use in the specs for running the linker.
  66.     Input files whose names have no recognized suffix are not compiled
  67.     at all, but they are included among the output files, so they will
  68.     be linked.
  69.  %p    substitutes the standard macro predefinitions for the
  70.     current target machine.  Use this when running cpp.
  71.  %P    like %p, but puts `__' before and after the name of each macro.
  72.     This is for ANSI C.
  73.  %s     current argument is the name of a library or startup file of some sort.
  74.         Search for that file in a standard list of directories
  75.     and substitute the full pathname found.
  76.  %eSTR  Print STR as an error message.  STR is terminated by a newline.
  77.         Use this when inconsistent options are detected.
  78.  %a     process ASM_SPEC as a spec.
  79.         This allows config.h to specify part of the spec for running as.
  80.  %l     process LINK_SPEC as a spec.
  81.  %L     process LIB_SPEC as a spec.
  82.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  83.  %c    process SIGNED_CHAR_SPEC as a spec.
  84.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  85.  %1    process CC1_SPEC as a spec.
  86.  %{S}   substitutes the -S switch, if that switch was given to CC.
  87.     If that switch was not specified, this substitutes nothing.
  88.     Here S is a metasyntactic variable.
  89.  %{S*}  substitutes all the switches specified to CC whose names start
  90.     with -S.  This is used for -o, -D, -I, etc; switches that take
  91.     arguments.  CC considers `-o foo' as being one switch whose
  92.     name starts with `o'.  %{o*} would substitute this text,
  93.     including the space; thus, two arguments would be generated.
  94.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  95.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  96.  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
  97.  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
  98.  
  99. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  100. other nested % constructs or spaces, or even newlines.
  101. They are processed as usual, as described above.
  102.  
  103. The character | is used to indicate that a command should be piped to
  104. the following command, but only if -pipe is specified.
  105.  
  106. Note that it is built into CC which switches take arguments and which
  107. do not.  You might think it would be useful to generalize this to
  108. allow each compiler's spec to say which switches take arguments.  But
  109. this cannot be done in a consistent fashion.  CC cannot even decide
  110. which input files have been specified without knowing which switches
  111. take arguments, and it must know which input files to compile in order
  112. to tell which compilers to run.
  113.  
  114. CC also knows implicitly that arguments starting in `-l' are to
  115. be treated as compiler output files, and passed to the linker in their proper
  116. position among the other output files.
  117.  
  118. */
  119.  
  120. #include <stdio.h>
  121. #include <sys/types.h>
  122. #include <signal.h>
  123. #include <sys/file.h>
  124.  
  125. #include "config.h"
  126. #include "obstack.h"
  127. #include "gvarargs.h"
  128.  
  129. #ifdef USG
  130. #define R_OK 4
  131. #define W_OK 2
  132. #define X_OK 1
  133. #define vfork fork
  134. #endif /* USG */
  135.  
  136. #define obstack_chunk_alloc xmalloc
  137. #define obstack_chunk_free free
  138. extern int xmalloc ();
  139. extern void free ();
  140.  
  141. /* If a stage of compilation returns an exit status >= 1,
  142.    compilation of that file ceases.  */
  143.  
  144. #define MIN_FATAL_STATUS 1
  145.  
  146. /* This is the obstack which we use to allocate many strings.  */
  147.  
  148. struct obstack obstack;
  149.  
  150. char *handle_braces ();
  151. char *save_string ();
  152. char *concat ();
  153. int do_spec ();
  154. int do_spec_1 ();
  155. char *find_file ();
  156. static char *find_exec_file ();
  157. void validate_switches ();
  158. void validate_all_switches ();
  159. void fancy_abort ();
  160.  
  161. /* config.h can define ASM_SPEC to provide extra args to the assembler
  162.    or extra switch-translations.  */
  163. #ifndef ASM_SPEC
  164. #define ASM_SPEC ""
  165. #endif
  166.  
  167. /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
  168.    or extra switch-translations.  */
  169. #ifndef CPP_SPEC
  170. #define CPP_SPEC ""
  171. #endif
  172.  
  173. /* config.h can define CC1_SPEC to provide extra args to cc1
  174.    or extra switch-translations.  */
  175. #ifndef CC1_SPEC
  176. #define CC1_SPEC ""
  177. #endif
  178.  
  179. /* config.h can define LINK_SPEC to provide extra args to the linker
  180.    or extra switch-translations.  */
  181. #ifndef LINK_SPEC
  182. #define LINK_SPEC ""
  183. #endif
  184.  
  185. /* config.h can define LIB_SPEC to override the default libraries.  */
  186. #ifndef LIB_SPEC
  187. #define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  188. #endif
  189.  
  190. /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
  191. #ifndef STARTFILE_SPEC
  192. #define STARTFILE_SPEC  \
  193.   "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
  194. #endif
  195.  
  196. /* This spec is used for telling cpp whether char is signed or not.  */
  197. #define SIGNED_CHAR_SPEC  \
  198.   (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}"    \
  199.    : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
  200.  
  201. /* This defines which switch letters take arguments.  */
  202.  
  203. #ifndef SWITCH_TAKES_ARG
  204. #define SWITCH_TAKES_ARG(CHAR)      \
  205.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  206.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  207.    || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
  208.    || (CHAR) == 'L' || (CHAR) == 'i')
  209. #endif
  210.  
  211. /* This defines which multi-letter switches take arguments.  */
  212.  
  213. #ifndef WORD_SWITCH_TAKES_ARG
  214. #define WORD_SWITCH_TAKES_ARG(STR) (!strcmp (STR, "Tdata"))
  215. #endif
  216.  
  217. /* This structure says how to run one compiler, and when to do so.  */
  218.  
  219. struct compiler
  220. {
  221.   char *suffix;            /* Use this compiler for input files
  222.                    whose names end in this suffix.  */
  223.   char *spec;            /* To use this compiler, pass this spec
  224.                    to do_spec.  */
  225. };
  226.  
  227. #define cplusplus_string \
  228.   "cpp -+ %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} \
  229.        -undef -D__GNUC__ -D__GNUG__ -D__cplusplus %p %P\
  230.        %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  231.        %{Wcomment*} %{Wtrigraphs} %{Wall} %C\
  232.        %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  233.    %{!M*:%{!E:cc1plus %{!pipe:%g.cpp} %1\
  234.          %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{+e*} %{a}\
  235.          %{g} %{g0} %{O} %{W*} %{w} %{pedantic} %{traditional}\
  236.          %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  237.          %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  238.          %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  239.          %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  240.         %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  241.                    %{!pipe:%g.s}\n }}}"
  242.  
  243. /* Here are the specs for compiling files with various known suffixes.
  244.    A file that does not end in any of these suffixes will be passed
  245.    unchanged to the loader and nothing else will be done to it.  */
  246.  
  247. struct compiler compilers[] =
  248. {
  249.   {".c",
  250. #if 0
  251.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} %{trigraphs} -undef \
  252.         -D__GNUC__ %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!ansi:%p} %P\
  253.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  254.     %{Wcomment*} %{Wtrigraphs} %{Wall} %C\
  255.         %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  256.     %{!M*:%{!E:cc1 %{!pipe:%g.cpp} %1 \
  257.            %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
  258.            %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  259.            %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  260.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  261.            %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  262.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  263.               %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  264.                       %{!pipe:%g.s}\n }}}"
  265. #else
  266.    cplusplus_string
  267. #endif
  268.    },
  269.  
  270.   {".cc",
  271.    cplusplus_string},
  272.   {".C",
  273.    cplusplus_string},
  274.   {".cxx",
  275.    cplusplus_string},
  276.  
  277.   {".i",
  278.    "cc1 %i %1 %{!Q:-quiet} %{Y*} %{d*} %{m*} %{f*} %{a}\
  279.     %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  280.     %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  281.     %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  282.     %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  283.             %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s}\n }"},
  284.   {".s",
  285.    "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  286.             %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i\n }"},
  287.   {".S",
  288.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{i*} %{M*} %{trigraphs} \
  289.         -undef -D__GNUC__ -$ %p %P\
  290.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  291.     %{Wcomment*} %{Wtrigraphs} %{Wall} %C\
  292.         %i %{!M*:%{!E:%{!pipe:%g.s}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  293.     %{!M*:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  294.                     %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  295.             %{!pipe:%g.s}\n }}}"},
  296.   /* Mark end of table */
  297.   {0, 0}
  298. };
  299.  
  300. #ifdef USE_COLLECT
  301. /* C++: Here is the spec for collecting global ctor and dtor
  302.    requirements.  */
  303. char *collect_spec =
  304.   "%{!c:%{!M*:%{!E:%{!S:collect -o %g.S %g.R\n\
  305. as %g.S -o %g.O\n\
  306. ld %{o*} %g.R %g.O\n\
  307. }}}}";
  308.  
  309. /* Here is the spec for running the linker, after compiling all files.  */
  310. char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld -r -o %g.R %l\
  311.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  312.  %{y*} %{!nostdlib:%S} \
  313.  %{L*} %o %{!nostdlib:-lg++ gnulib%s %{g:-lg} %L}\n }}}}";
  314. #else
  315. /* Here is the spec for running the linker, after compiling all files.  */
  316. char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\
  317.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  318.  %{y*} %{!nostdlib:%S} \
  319.  %{L*} %o %{!nostdlib:-lg++ gnulib%s %{g:-lg} %L}\n }}}}";
  320. #endif
  321.  
  322. /* Accumulate a command (program name and args), and run it.  */
  323.  
  324. /* Vector of pointers to arguments in the current line of specifications.  */
  325.  
  326. char **argbuf;
  327.  
  328. /* Number of elements allocated in argbuf.  */
  329.  
  330. int argbuf_length;
  331.  
  332. /* Number of elements in argbuf currently in use (containing args).  */
  333.  
  334. int argbuf_index;
  335.  
  336. /* Number of commands executed so far.  */
  337.  
  338. int execution_count;
  339.  
  340. /* Flag indicating whether we should print the command and arguments */
  341.  
  342. unsigned char vflag;
  343.  
  344. /* Name with which this program was invoked.  */
  345.  
  346. char *programname;
  347.  
  348. /* User-specified -B prefix to attach to command names,
  349.    or 0 if none specified.  */
  350.  
  351. char *user_exec_prefix = 0;
  352.  
  353. /* Environment-specified prefix to attach to command names,
  354.    or 0 if none specified.  */
  355.  
  356. char *env_exec_prefix = 0;
  357.  
  358. /* Suffix to attach to directories searched for commands.  */
  359.  
  360. char *machine_suffix = 0;
  361.  
  362. /* Default prefixes to attach to command names.  */
  363.  
  364. #ifndef STANDARD_EXEC_PREFIX
  365. #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"
  366. #endif /* !defined STANDARD_EXEC_PREFIX */
  367.  
  368. char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
  369. char *standard_exec_prefix_1 = "/usr/lib/gcc-";
  370.  
  371. #ifndef STANDARD_STARTFILE_PREFIX
  372. #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
  373. #endif /* !defined STANDARD_STARTFILE_PREFIX */
  374.  
  375. char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
  376. char *standard_startfile_prefix_1 = "/lib/";
  377. char *standard_startfile_prefix_2 = "/usr/lib/";
  378.  
  379. /* Clear out the vector of arguments (after a command is executed).  */
  380.  
  381. void
  382. clear_args ()
  383. {
  384.   argbuf_index = 0;
  385. }
  386.  
  387. /* Add one argument to the vector at the end.
  388.    This is done when a space is seen or at the end of the line.
  389.    If DELETE_ALWAYS is nonzero, the arg is a filename
  390.     and the file should be deleted eventually.
  391.    If DELETE_FAILURE is nonzero, the arg is a filename
  392.     and the file should be deleted if this compilation fails.  */
  393.  
  394. void
  395. store_arg (arg, delete_always, delete_failure)
  396.      char *arg;
  397.      int delete_always, delete_failure;
  398. {
  399.   if (argbuf_index + 1 == argbuf_length)
  400.     {
  401.       argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  402.     }
  403.  
  404.   argbuf[argbuf_index++] = arg;
  405.   argbuf[argbuf_index] = 0;
  406.  
  407.   if (delete_always || delete_failure)
  408.     record_temp_file (arg, delete_always, delete_failure);
  409. }
  410.  
  411. /* Record the names of temporary files we tell compilers to write,
  412.    and delete them at the end of the run.  */
  413.  
  414. /* This is the common prefix we use to make temp file names.
  415.    It is chosen once for each run of this program.
  416.    It is substituted into a spec by %g.
  417.    Thus, all temp file names contain this prefix.
  418.    In practice, all temp file names start with this prefix.
  419.  
  420.    This prefix comes from the envvar TMPDIR if it is defined;
  421.    otherwise, from the P_tmpdir macro if that is defined;
  422.    otherwise, in /usr/tmp or /tmp.  */
  423.  
  424. char *temp_filename;
  425.  
  426. /* Length of the prefix.  */
  427.  
  428. int temp_filename_length;
  429.  
  430. /* Define the list of temporary files to delete.  */
  431.  
  432. struct temp_file
  433. {
  434.   char *name;
  435.   struct temp_file *next;
  436. };
  437.  
  438. /* Queue of files to delete on success or failure of compilation.  */
  439. struct temp_file *always_delete_queue;
  440. /* Queue of files to delete on failure of compilation.  */
  441. struct temp_file *failure_delete_queue;
  442.  
  443. /* Record FILENAME as a file to be deleted automatically.
  444.    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
  445.    otherwise delete it in any case.
  446.    FAIL_DELETE nonzero means delete it if a compilation step fails;
  447.    otherwise delete it in any case.  */
  448.  
  449. void
  450. record_temp_file (filename, always_delete, fail_delete)
  451.      char *filename;
  452.      int always_delete;
  453.      int fail_delete;
  454. {
  455.   register char *name;
  456.   name = (char *) xmalloc (strlen (filename) + 1);
  457.   strcpy (name, filename);
  458.  
  459.   if (always_delete)
  460.     {
  461.       register struct temp_file *temp;
  462.       for (temp = always_delete_queue; temp; temp = temp->next)
  463.     if (! strcmp (name, temp->name))
  464.       goto already1;
  465.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  466.       temp->next = always_delete_queue;
  467.       temp->name = name;
  468.       always_delete_queue = temp;
  469.     already1:;
  470.     }
  471.  
  472.   if (fail_delete)
  473.     {
  474.       register struct temp_file *temp;
  475.       for (temp = failure_delete_queue; temp; temp = temp->next)
  476.     if (! strcmp (name, temp->name))
  477.       goto already2;
  478.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  479.       temp->next = failure_delete_queue;
  480.       temp->name = name;
  481.       failure_delete_queue = temp;
  482.     already2:;
  483.     }
  484. }
  485.  
  486. /* Delete all the temporary files whose names we previously recorded.  */
  487.  
  488. void
  489. delete_temp_files ()
  490. {
  491.   register struct temp_file *temp;
  492.  
  493.   for (temp = always_delete_queue; temp; temp = temp->next)
  494.     {
  495. #ifdef DEBUG
  496.       int i;
  497.       printf ("Delete %s? (y or n) ", temp->name);
  498.       fflush (stdout);
  499.       i = getchar ();
  500.       if (i != '\n')
  501.     while (getchar () != '\n') ;
  502.       if (i == 'y' || i == 'Y')
  503. #endif /* DEBUG */
  504.     {
  505.       if (unlink (temp->name) < 0)
  506.         if (vflag)
  507.           perror_with_name (temp->name);
  508.     }
  509.     }
  510.  
  511.   always_delete_queue = 0;
  512. }
  513.  
  514. /* Delete all the files to be deleted on error.  */
  515.  
  516. void
  517. delete_failure_queue ()
  518. {
  519.   register struct temp_file *temp;
  520.  
  521.   for (temp = failure_delete_queue; temp; temp = temp->next)
  522.     {
  523. #ifdef DEBUG
  524.       int i;
  525.       printf ("Delete %s? (y or n) ", temp->name);
  526.       fflush (stdout);
  527.       i = getchar ();
  528.       if (i != '\n')
  529.     while (getchar () != '\n') ;
  530.       if (i == 'y' || i == 'Y')
  531. #endif /* DEBUG */
  532.     {
  533.       if (unlink (temp->name) < 0)
  534.         if (vflag)
  535.           perror_with_name (temp->name);
  536.     }
  537.     }
  538. }
  539.  
  540. void
  541. clear_failure_queue ()
  542. {
  543.   failure_delete_queue = 0;
  544. }
  545.  
  546. /* Compute a string to use as the base of all temporary file names.
  547.    It is substituted for %g.  */
  548.  
  549. void
  550. choose_temp_base ()
  551. {
  552.   extern char *getenv ();
  553.   char *base = getenv ("TMPDIR");
  554.   int len;
  555.  
  556.   if (base == (char *)0)
  557.     {
  558. #ifdef P_tmpdir
  559.       if (access (P_tmpdir, R_OK | W_OK) == 0)
  560.     base = P_tmpdir;
  561. #endif
  562.       if (base == (char *)0)
  563.     {
  564.       if (access ("/usr/tmp", R_OK | W_OK) == 0)
  565.         base = "/usr/tmp/";
  566.       else
  567.         base = "/tmp/";
  568.     }
  569.     }
  570.  
  571.   len = strlen (base);
  572.   temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));
  573.   strcpy (temp_filename, base);
  574.   if (len > 0 && temp_filename[len-1] != '/')
  575.     temp_filename[len++] = '/';
  576.   strcpy (temp_filename + len, "ccXXXXXX");
  577.  
  578.   mktemp (temp_filename);
  579.   temp_filename_length = strlen (temp_filename);
  580. }
  581.  
  582. /* Search for an execute file through our search path.
  583.    Return 0 if not found, otherwise return its name, allocated with malloc.  */
  584.  
  585. static char *
  586. find_exec_file (prog)
  587.      char *prog;
  588. {
  589.   int win = 0;
  590.   char *temp;
  591.   int size;
  592.  
  593.   size = strlen (standard_exec_prefix);
  594.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  595.     size = strlen (user_exec_prefix);
  596.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  597.     size = strlen (env_exec_prefix);
  598.   if (strlen (standard_exec_prefix_1) > size)
  599.     size = strlen (standard_exec_prefix_1);
  600.   size += strlen (prog) + 1;
  601.   if (machine_suffix)
  602.     size += strlen (machine_suffix) + 1;
  603.   temp = (char *) xmalloc (size);
  604.  
  605.   /* Determine the filename to execute.  */
  606.  
  607.   if (user_exec_prefix)
  608.     {
  609.       if (machine_suffix)
  610.     {
  611.       strcpy (temp, user_exec_prefix);
  612.       strcat (temp, machine_suffix);
  613.       strcat (temp, prog);
  614.       win = (access (temp, X_OK) == 0);
  615.     }
  616.       if (!win)
  617.     {
  618.       strcpy (temp, user_exec_prefix);
  619.       strcat (temp, prog);
  620.       win = (access (temp, X_OK) == 0);
  621.     }
  622.     }
  623.  
  624.   if (!win && env_exec_prefix)
  625.     {
  626.       if (machine_suffix)
  627.     {
  628.       strcpy (temp, env_exec_prefix);
  629.       strcat (temp, machine_suffix);
  630.       strcat (temp, prog);
  631.       win = (access (temp, X_OK) == 0);
  632.     }
  633.       if (!win)
  634.     {
  635.       strcpy (temp, env_exec_prefix);
  636.       strcat (temp, prog);
  637.       win = (access (temp, X_OK) == 0);
  638.     }
  639.     }
  640.  
  641.   if (!win)
  642.     {
  643.       if (machine_suffix)
  644.     {
  645.       strcpy (temp, standard_exec_prefix);
  646.       strcat (temp, machine_suffix);
  647.       strcat (temp, prog);
  648.       win = (access (temp, X_OK) == 0);
  649.     }
  650.       if (!win)
  651.     {
  652.       strcpy (temp, standard_exec_prefix);
  653.       strcat (temp, prog);
  654.       win = (access (temp, X_OK) == 0);
  655.     }
  656.     }
  657.  
  658.   if (!win)
  659.     {
  660.       if (machine_suffix)
  661.     {
  662.       strcpy (temp, standard_exec_prefix_1);
  663.       strcat (temp, machine_suffix);
  664.       strcat (temp, prog);
  665.       win = (access (temp, X_OK) == 0);
  666.     }
  667.       if (!win)
  668.     {
  669.       strcpy (temp, standard_exec_prefix_1);
  670.       strcat (temp, prog);
  671.       win = (access (temp, X_OK) == 0);
  672.     }
  673.     }
  674.  
  675.   if (win)
  676.     return temp;
  677.   else
  678.     return 0;
  679. }
  680.  
  681. /* stdin file number.  */
  682. #define STDIN_FILE_NO 0
  683.  
  684. /* stdout file number.  */
  685. #define STDOUT_FILE_NO 1
  686.  
  687. /* value of `pipe': port index for reading.  */
  688. #define READ_PORT 0
  689.  
  690. /* value of `pipe': port index for writing.  */
  691. #define WRITE_PORT 1
  692.  
  693. /* Pipe waiting from last process, to be used as input for the next one.
  694.    Value is STDIN_FILE_NO if no pipe is waiting
  695.    (i.e. the next command is the first of a group).  */
  696.  
  697. int last_pipe_input;
  698.  
  699. /* Fork one piped subcommand.  FUNC is the system call to use
  700.    (either execv or execvp).  ARGV is the arg vector to use.
  701.    NOT_LAST is nonzero if this is not the last subcommand
  702.    (i.e. its output should be piped to the next one.)  */
  703.  
  704. static int
  705. pexecute (func, program, argv, not_last)
  706.      char *program;
  707.      int (*func)();
  708.      char *argv[];
  709.      int not_last;
  710. {
  711.   int pid;
  712.   int pdes[2];
  713.   int input_desc = last_pipe_input;
  714.   int output_desc = STDOUT_FILE_NO;
  715.  
  716.   /* If this isn't the last process, make a pipe for its output,
  717.      and record it as waiting to be the input to the next process.  */
  718.  
  719.   if (not_last)
  720.     {
  721.       if (pipe (pdes) < 0)
  722.     pfatal_with_name ("pipe");
  723.       output_desc = pdes[WRITE_PORT];
  724.       last_pipe_input = pdes[READ_PORT];
  725.     }
  726.   else
  727.     last_pipe_input = STDIN_FILE_NO;
  728.  
  729.   pid = vfork ();
  730.  
  731.   switch (pid)
  732.     {
  733.     case -1:
  734.       pfatal_with_name ("vfork");
  735.       break;
  736.  
  737.     case 0: /* child */
  738.       /* Move the input and output pipes into place, if nec.  */
  739.       if (input_desc != STDIN_FILE_NO)
  740.     {
  741.       close (STDIN_FILE_NO);
  742.       dup (input_desc);
  743.       close (input_desc);
  744.     }
  745.       if (output_desc != STDOUT_FILE_NO)
  746.     {
  747.       close (STDOUT_FILE_NO);
  748.       dup (output_desc);
  749.       close (output_desc);
  750.     }
  751.  
  752.       /* Close the parent's descs that aren't wanted here.  */
  753.       if (last_pipe_input != STDIN_FILE_NO)
  754.     close (last_pipe_input);
  755.  
  756.       /* Exec the program.  */
  757.       (*func) (program, argv);
  758.       perror_exec (program);
  759.       exit (-1);
  760.       /* NOTREACHED */
  761.  
  762.     default:
  763.       /* In the parent, after forking.
  764.      Close the descriptors that we made for this child.  */
  765.       if (input_desc != STDIN_FILE_NO)
  766.     close (input_desc);
  767.       if (output_desc != STDOUT_FILE_NO)
  768.     close (output_desc);
  769.  
  770.       /* Return child's process number.  */
  771.       return pid;
  772.     }
  773. }
  774.  
  775. /* Execute the command specified by the arguments on the current line of spec.
  776.    When using pipes, this includes several piped-together commands
  777.    with `|' between them.
  778.  
  779.    Return 0 if successful, -1 if failed.  */
  780.  
  781. int
  782. execute ()
  783. {
  784.   int i;
  785.   int n_commands;        /* # of command.  */
  786.   char *string;
  787.   struct command
  788.     {
  789.       char *prog;        /* program name.  */
  790.       char **argv;        /* vector of args.  */
  791.       int pid;            /* pid of process for this command.  */
  792.     };
  793.  
  794.   struct command *commands;    /* each command buffer with above info.  */
  795.  
  796.   /* Count # of piped commands.  */
  797.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  798.     if (strcmp (argbuf[i], "|") == 0)
  799.       n_commands++;
  800.  
  801.   /* Get storage for each command.  */
  802.   commands
  803.     = (struct command *) alloca (n_commands * sizeof (struct command));
  804.  
  805.   /* Split argbuf into its separate piped processes,
  806.      and record info about each one.
  807.      Also search for the programs that are to be run.  */
  808.  
  809.   commands[0].prog = argbuf[0]; /* first command.  */
  810.   commands[0].argv = &argbuf[0];
  811.   string = find_exec_file (commands[0].prog);
  812.   if (string)
  813.     commands[0].argv[0] = string;
  814.  
  815.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  816.     if (strcmp (argbuf[i], "|") == 0)
  817.       {                /* each command.  */
  818.     argbuf[i] = 0;    /* termination of command args.  */
  819.     commands[n_commands].prog = argbuf[i + 1];
  820.     commands[n_commands].argv = &argbuf[i + 1];
  821.     string = find_exec_file (commands[n_commands].prog);
  822.     if (string)
  823.       commands[n_commands].argv[0] = string;
  824.     n_commands++;
  825.       }
  826.  
  827.   argbuf[argbuf_index] = 0;
  828.  
  829.   /* If -v, print what we are about to do, and maybe query.  */
  830.  
  831.   if (vflag)
  832.     {
  833.       /* Print each piped command as a separate line.  */
  834.       for (i = 0; i < n_commands ; i++)
  835.     {
  836.       char **j;
  837.  
  838.       for (j = commands[i].argv; *j; j++)
  839.         fprintf (stderr, " %s", *j);
  840.  
  841.       /* Print a pipe symbol after all but the last command.  */
  842.       if (i + 1 != n_commands)
  843.         fprintf (stderr, " |");
  844.       fprintf (stderr, "\n");
  845.     }
  846.       fflush (stderr);
  847. #ifdef DEBUG
  848.       fprintf (stderr, "\nGo ahead? (y or n) ");
  849.       fflush (stderr);
  850.       j = getchar ();
  851.       if (j != '\n')
  852.     while (getchar () != '\n') ;
  853.       if (j != 'y' && j != 'Y')
  854.     return 0;
  855. #endif /* DEBUG */
  856.     }
  857.  
  858.   /* Run each piped subprocess.  */
  859.  
  860.   last_pipe_input = STDIN_FILE_NO;
  861.   for (i = 0; i < n_commands; i++)
  862.     {
  863.       extern int execv(), execvp();
  864.       char *string = commands[i].argv[0];
  865.  
  866.       commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
  867.                   string, commands[i].argv,
  868.                   i + 1 < n_commands);
  869.  
  870.       if (string != commands[i].prog)
  871.     free (string);
  872.     }
  873.  
  874.   execution_count++;
  875.  
  876.   /* Wait for all the subprocesses to finish.
  877.      We don't care what order they finish in;
  878.      we know that N_COMMANDS waits will get them all.  */
  879.  
  880.   {
  881.     int ret_code = 0;
  882.  
  883.     for (i = 0; i < n_commands; i++)
  884.       {
  885.     int status;
  886.     int pid;
  887.     char *prog;
  888.  
  889.     pid = wait (&status);
  890.     if (pid < 0)
  891.       abort ();
  892.  
  893.     if (status != 0)
  894.       {
  895.         int j;
  896.         for (j = 0; j < n_commands; j++)
  897.           if (commands[j].pid == pid)
  898.         prog = commands[j].prog;
  899.  
  900.         if ((status & 0x7F) != 0)
  901.           fatal ("Program %s got fatal signal %d.", prog, (status & 0x7F));
  902.         if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  903.           ret_code = -1;
  904.       }
  905.       }
  906.     return ret_code;
  907.   }
  908. }
  909.  
  910. /* Find all the switches given to us
  911.    and make a vector describing them.
  912.  
  913.    The elements of the vector a strings, one per switch given.
  914.    If a switch uses the following argument, then the `part1' field
  915.    is the switch itself and the `part2' field is the following argument.
  916.    The `valid' field is nonzero if any spec has looked at this switch;
  917.    if it remains zero at the end of the run, it must be meaningless.
  918.  
  919.    `prefix' is the prefix of this switch which makes it
  920.    recognizable as a switch.  This is almost always "-".  When
  921.    it is NULL, it means that the prefix to this switch is part
  922.    `part1' instead.  */
  923.  
  924. struct switchstr
  925. {
  926.   char *prefix;
  927.   char *part1;
  928.   char *part2;
  929.   int valid;
  930. };
  931.  
  932. struct switchstr *switches;
  933.  
  934. int n_switches;
  935.  
  936. /* Also a vector of input files specified.  */
  937.  
  938. char **infiles;
  939.  
  940. int n_infiles;
  941.  
  942. /* And a vector of corresponding output files is made up later.  */
  943.  
  944. char **outfiles;
  945.  
  946. /* Create the vector `switches' and its contents.
  947.    Store its length in `n_switches'.  */
  948.  
  949. void
  950. process_command (argc, argv)
  951.      int argc;
  952.      char **argv;
  953. {
  954.   extern char *getenv ();
  955.   register int i;
  956.   n_switches = 0;
  957.   n_infiles = 0;
  958.  
  959.   env_exec_prefix = getenv ("GCC_EXEC_PREFIX");
  960.  
  961.   /* Scan argv twice.  Here, the first time, just count how many switches
  962.      there will be in their vector, and how many input files in theirs.
  963.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  964.  
  965.   for (i = 1; i < argc; i++)
  966.     {
  967.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  968.     {
  969.       register char *p = &argv[i][1];
  970.       register int c = *p;
  971.  
  972.       switch (c)
  973.         {
  974.         case 'b':
  975.           machine_suffix = p + 1;
  976.           break;
  977.  
  978.         case 'B':
  979.           user_exec_prefix = p + 1;
  980.           break;
  981.  
  982.         case 'v':    /* Print our subcommands and print versions.  */
  983.           vflag++;
  984.           n_switches++;
  985.           break;
  986.  
  987.         default:
  988.           n_switches++;
  989.  
  990.           if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  991.         i++;
  992.           else if (WORD_SWITCH_TAKES_ARG (p))
  993.         i++;
  994.         }
  995.     }
  996.       else if (argv[i][0] == '+')
  997.     ;
  998.       else
  999.     n_infiles++;
  1000.     }
  1001.  
  1002.   /* Then create the space for the vectors and scan again.  */
  1003.  
  1004.   switches = ((struct switchstr *)
  1005.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  1006.   infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
  1007.   n_switches = 0;
  1008.   n_infiles = 0;
  1009.  
  1010.   /* This, time, copy the text of each switch and store a pointer
  1011.      to the copy in the vector of switches.
  1012.      Store all the infiles in their vector.  */
  1013.  
  1014.   for (i = 1; i < argc; i++)
  1015.     {
  1016.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  1017.     {
  1018.       register char *p = &argv[i][1];
  1019.       register int c = *p;
  1020.  
  1021.       if (c == 'B' || c == 'b')
  1022.         continue;
  1023.       switches[n_switches].prefix = "-";
  1024.       switches[n_switches].part1 = p;
  1025.       if ((SWITCH_TAKES_ARG (c) && p[1] == 0)
  1026.           || WORD_SWITCH_TAKES_ARG (p))
  1027.         switches[n_switches].part2 = argv[++i];
  1028.       else
  1029.         switches[n_switches].part2 = 0;
  1030.       switches[n_switches].valid = 0;
  1031.       n_switches++;
  1032.     }
  1033.       else if (argv[i][0] == '+')
  1034.     {
  1035.       switches[n_switches].prefix = "";
  1036.       switches[n_switches].part1 = argv[i];
  1037.       switches[n_switches].part2 = 0;
  1038.       switches[n_switches].valid = 0;
  1039.       n_switches++;
  1040.     }
  1041.       else
  1042.     infiles[n_infiles++] = argv[i];
  1043.     }
  1044.  
  1045.   switches[n_switches].part1 = 0;
  1046.   infiles[n_infiles] = 0;
  1047. }
  1048.  
  1049. /* Process a spec string, accumulating and running commands.  */
  1050.  
  1051. /* These variables describe the input file name.
  1052.    input_file_number is the index on outfiles of this file,
  1053.    so that the output file name can be stored for later use by %o.
  1054.    input_basename is the start of the part of the input file
  1055.    sans all directory names, and basename_length is the number
  1056.    of characters starting there excluding the suffix .c or whatever.  */
  1057.  
  1058. char *input_filename;
  1059. int input_file_number;
  1060. int input_filename_length;
  1061. int basename_length;
  1062. char *input_basename;
  1063.  
  1064. /* These are variables used within do_spec and do_spec_1.  */
  1065.  
  1066. /* Nonzero if an arg has been started and not yet terminated
  1067.    (with space, tab or newline).  */
  1068. int arg_going;
  1069.  
  1070. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  1071.    is a temporary file name.  */
  1072. int delete_this_arg;
  1073.  
  1074. /* Nonzero means %w has been seen; the next arg to be terminated
  1075.    is the output file name of this compilation.  */
  1076. int this_is_output_file;
  1077.  
  1078. /* Nonzero means %s has been seen; the next arg to be terminated
  1079.    is the name of a library file and we should try the standard
  1080.    search dirs for it.  */
  1081. int this_is_library_file;
  1082.  
  1083. /* Process the spec SPEC and run the commands specified therein.
  1084.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  1085.  
  1086. int
  1087. do_spec (spec)
  1088.      char *spec;
  1089. {
  1090.   int value;
  1091.  
  1092.   clear_args ();
  1093.   arg_going = 0;
  1094.   delete_this_arg = 0;
  1095.   this_is_output_file = 0;
  1096.   this_is_library_file = 0;
  1097.  
  1098.   value = do_spec_1 (spec, 0);
  1099.  
  1100.   /* Force out any unfinished command.
  1101.      If -pipe, this forces out the last command if it ended in `|'.  */
  1102.   if (value == 0)
  1103.     {
  1104.       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1105.     argbuf_index--;
  1106.  
  1107.       if (argbuf_index > 0)
  1108.     value = execute ();
  1109.     }
  1110.  
  1111.   return value;
  1112. }
  1113.  
  1114. /* Process the sub-spec SPEC as a portion of a larger spec.
  1115.    This is like processing a whole spec except that we do
  1116.    not initialize at the beginning and we do not supply a
  1117.    newline by default at the end.
  1118.    INSWITCH nonzero means don't process %-sequences in SPEC;
  1119.    in this case, % is treated as an ordinary character.
  1120.    This is used while substituting switches.
  1121.    INSWITCH nonzero also causes SPC not to terminate an argument.
  1122.  
  1123.    Value is zero unless a line was finished
  1124.    and the command on that line reported an error.  */
  1125.  
  1126. int
  1127. do_spec_1 (spec, inswitch)
  1128.      char *spec;
  1129.      int inswitch;
  1130. {
  1131.   register char *p = spec;
  1132.   register int c;
  1133.   char *string;
  1134.  
  1135.   while (c = *p++)
  1136.     /* If substituting a switch, treat all chars like letters.
  1137.        Otherwise, NL, SPC, TAB and % are special.  */
  1138.     switch (inswitch ? 'a' : c)
  1139.       {
  1140.       case '\n':
  1141.     /* End of line: finish any pending argument,
  1142.        then run the pending command if one has been started.  */
  1143.     if (arg_going)
  1144.       {
  1145.         obstack_1grow (&obstack, 0);
  1146.         string = obstack_finish (&obstack);
  1147.         if (this_is_library_file)
  1148.           string = find_file (string);
  1149.         store_arg (string, delete_this_arg, this_is_output_file);
  1150.         if (this_is_output_file)
  1151.           outfiles[input_file_number] = string;
  1152.       }
  1153.     arg_going = 0;
  1154.  
  1155.     if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1156.       {
  1157.         int i;
  1158.         for (i = 0; i < n_switches; i++)
  1159.           if (!strcmp (switches[i].part1, "pipe"))
  1160.         break;
  1161.  
  1162.         /* A `|' before the newline means use a pipe here,
  1163.            but only if -pipe was specified.
  1164.            Otherwise, execute now and don't pass the `|' as an arg.  */
  1165.         if (i < n_switches)
  1166.           {
  1167.         switches[i].valid = 1;
  1168.         break;
  1169.           }
  1170.         else
  1171.           argbuf_index--;
  1172.       }
  1173.  
  1174.     if (argbuf_index > 0)
  1175.       {
  1176.         int value = execute ();
  1177.         if (value)
  1178.           return value;
  1179.       }
  1180.     /* Reinitialize for a new command, and for a new argument.  */
  1181.     clear_args ();
  1182.     arg_going = 0;
  1183.     delete_this_arg = 0;
  1184.     this_is_output_file = 0;
  1185.     this_is_library_file = 0;
  1186.     break;
  1187.  
  1188.       case '|':
  1189.     /* End any pending argument.  */
  1190.     if (arg_going)
  1191.       {
  1192.         obstack_1grow (&obstack, 0);
  1193.         string = obstack_finish (&obstack);
  1194.         if (this_is_library_file)
  1195.           string = find_file (string);
  1196.         store_arg (string, delete_this_arg, this_is_output_file);
  1197.         if (this_is_output_file)
  1198.           outfiles[input_file_number] = string;
  1199.       }
  1200.  
  1201.     /* Use pipe */
  1202.     obstack_1grow (&obstack, c);
  1203.     arg_going = 1;
  1204.     break;
  1205.  
  1206.       case '\t':
  1207.       case ' ':
  1208.     /* Space or tab ends an argument if one is pending.  */
  1209.     if (arg_going)
  1210.       {
  1211.         obstack_1grow (&obstack, 0);
  1212.         string = obstack_finish (&obstack);
  1213.         if (this_is_library_file)
  1214.           string = find_file (string);
  1215.         store_arg (string, delete_this_arg, this_is_output_file);
  1216.         if (this_is_output_file)
  1217.           outfiles[input_file_number] = string;
  1218.       }
  1219.     /* Reinitialize for a new argument.  */
  1220.     arg_going = 0;
  1221.     delete_this_arg = 0;
  1222.     this_is_output_file = 0;
  1223.     this_is_library_file = 0;
  1224.     break;
  1225.  
  1226.       case '%':
  1227.     switch (c = *p++)
  1228.       {
  1229.       case 0:
  1230.         fatal ("Invalid specification!  Bug in cc.");
  1231.  
  1232.       case 'b':
  1233.         obstack_grow (&obstack, input_basename, basename_length);
  1234.         arg_going = 1;
  1235.         break;
  1236.  
  1237.       case 'd':
  1238.         delete_this_arg = 2;
  1239.         break;
  1240.  
  1241.       case 'e':
  1242.         /* {...:%efoo} means report an error with `foo' as error message
  1243.            and don't execute any more commands for this file.  */
  1244.         {
  1245.           char *q = p;
  1246.           char *buf;
  1247.           while (*p != 0 && *p != '\n') p++;
  1248.           buf = (char *) alloca (p - q + 1);
  1249.           strncpy (buf, q, p - q);
  1250.           error ("%s", buf);
  1251.           return -1;
  1252.         }
  1253.         break;
  1254.  
  1255.       case 'g':
  1256.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  1257.         delete_this_arg = 1;
  1258.         arg_going = 1;
  1259.         break;
  1260.  
  1261.       case 'i':
  1262.         obstack_grow (&obstack, input_filename, input_filename_length);
  1263.         arg_going = 1;
  1264.         break;
  1265.  
  1266.       case 'o':
  1267.         {
  1268.           register int f;
  1269.           for (f = 0; f < n_infiles; f++)
  1270.         store_arg (outfiles[f], 0, 0);
  1271.         }
  1272.         break;
  1273.  
  1274.       case 's':
  1275.         this_is_library_file = 1;
  1276.         break;
  1277.  
  1278.       case 'w':
  1279.         this_is_output_file = 1;
  1280.         break;
  1281.  
  1282.       case '{':
  1283.         p = handle_braces (p);
  1284.         if (p == 0)
  1285.           return -1;
  1286.         break;
  1287.  
  1288.       case '%':
  1289.         obstack_1grow (&obstack, '%');
  1290.         break;
  1291.  
  1292. /*** The rest just process a certain constant string as a spec.  */
  1293.  
  1294.       case '1':
  1295.         do_spec_1 (CC1_SPEC, 0);
  1296.         break;
  1297.  
  1298.       case 'a':
  1299.         do_spec_1 (ASM_SPEC, 0);
  1300.         break;
  1301.  
  1302.       case 'c':
  1303.         do_spec_1 (SIGNED_CHAR_SPEC, 0);
  1304.         break;
  1305.  
  1306.       case 'C':
  1307.         do_spec_1 (CPP_SPEC, 0);
  1308.         break;
  1309.  
  1310.       case 'l':
  1311.         do_spec_1 (LINK_SPEC, 0);
  1312.         break;
  1313.  
  1314.       case 'L':
  1315.         do_spec_1 (LIB_SPEC, 0);
  1316.         break;
  1317.  
  1318.       case 'p':
  1319.         do_spec_1 (CPP_PREDEFINES, 0);
  1320.         break;
  1321.  
  1322.       case 'P':
  1323.         {
  1324.           char *x = (char *) alloca (strlen (CPP_PREDEFINES) * 2 + 1);
  1325.           char *buf = x;
  1326.           char *y = CPP_PREDEFINES;
  1327.  
  1328.           /* Copy all of CPP_PREDEFINES into BUF,
  1329.          but put __ after every -D and at the end of each arg,  */
  1330.           while (1)
  1331.         {
  1332.           if (! strncmp (y, "-D", 2))
  1333.             {
  1334.               *x++ = '-';
  1335.               *x++ = 'D';
  1336.               *x++ = '_';
  1337.               *x++ = '_';
  1338.               y += 2;
  1339.             }
  1340.           else if (*y == ' ' || *y == 0)
  1341.             {
  1342.               *x++ = '_';
  1343.               *x++ = '_';
  1344.               if (*y == 0)
  1345.             break;
  1346.               else
  1347.             *x++ = *y++;
  1348.             }
  1349.           else
  1350.             *x++ = *y++;
  1351.         }
  1352.           *x = 0;
  1353.  
  1354.           do_spec_1 (buf, 0);
  1355.         }
  1356.         break;
  1357.  
  1358.       case 'S':
  1359.         do_spec_1 (STARTFILE_SPEC, 0);
  1360.         break;
  1361.  
  1362.       default:
  1363.         abort ();
  1364.       }
  1365.     break;
  1366.  
  1367.       default:
  1368.     /* Ordinary character: put it into the current argument.  */
  1369.     obstack_1grow (&obstack, c);
  1370.     arg_going = 1;
  1371.       }
  1372.  
  1373.   return 0;        /* End of string */
  1374. }
  1375.  
  1376. /* Return 0 if we call do_spec_1 and that returns -1.  */
  1377.  
  1378. char *
  1379. handle_braces (p)
  1380.      register char *p;
  1381. {
  1382.   register char *q;
  1383.   char *filter;
  1384.   int pipe = 0;
  1385.   int negate = 0;
  1386.  
  1387.   if (*p == '|')
  1388.     /* A `|' after the open-brace means,
  1389.        if the test fails, output a single minus sign rather than nothing.
  1390.        This is used in %{|!pipe:...}.  */
  1391.     pipe = 1, ++p;
  1392.  
  1393.   if (*p == '!')
  1394.     /* A `!' after the open-brace negates the condition:
  1395.        succeed if the specified switch is not present.  */
  1396.     negate = 1, ++p;
  1397.  
  1398.   filter = p;
  1399.   while (*p != ':' && *p != '}') p++;
  1400.   if (*p != '}')
  1401.     {
  1402.       register int count = 1;
  1403.       q = p + 1;
  1404.       while (count > 0)
  1405.     {
  1406.       if (*q == '{')
  1407.         count++;
  1408.       else if (*q == '}')
  1409.         count--;
  1410.       else if (*q == 0)
  1411.         abort ();
  1412.       q++;
  1413.     }
  1414.     }
  1415.   else
  1416.     q = p + 1;
  1417.  
  1418.   if (p[-1] == '*' && p[0] == '}')
  1419.     {
  1420.       /* Substitute all matching switches as separate args.  */
  1421.       register int i;
  1422.       --p;
  1423.       for (i = 0; i < n_switches; i++)
  1424.     if (!strncmp (switches[i].part1, filter, p - filter))
  1425.       give_switch (i);
  1426.     }
  1427.   else
  1428.     {
  1429.       /* Test for presence of the specified switch.  */
  1430.       register int i;
  1431.       int present = 0;
  1432.  
  1433.       /* If name specified ends in *, as in {x*:...},
  1434.      check for presence of any switch name starting with x.  */
  1435.       if (p[-1] == '*')
  1436.     {
  1437.       for (i = 0; i < n_switches; i++)
  1438.         {
  1439.           if (!strncmp (switches[i].part1, filter, p - filter - 1))
  1440.         {
  1441.           switches[i].valid = 1;
  1442.           present = 1;
  1443.         }
  1444.         }
  1445.     }
  1446.       /* Otherwise, check for presence of exact name specified.  */
  1447.       else
  1448.     {
  1449.       for (i = 0; i < n_switches; i++)
  1450.         {
  1451.           if (!strncmp (switches[i].part1, filter, p - filter)
  1452.           && switches[i].part1[p - filter] == 0)
  1453.         {
  1454.           switches[i].valid = 1;
  1455.           present = 1;
  1456.           break;
  1457.         }
  1458.         }
  1459.     }
  1460.  
  1461.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  1462.      then substitute either the switch or the specified
  1463.      conditional text.  */
  1464.       if (present != negate)
  1465.     {
  1466.       if (*p == '}')
  1467.         {
  1468.           give_switch (i);
  1469.         }
  1470.       else
  1471.         {
  1472.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
  1473.         return 0;
  1474.         }
  1475.     }
  1476.       else if (pipe)
  1477.     {
  1478.       /* Here if a %{|...} conditional fails: output a minus sign,
  1479.          which means "standard output" or "standard input".  */
  1480.       do_spec_1 ("-", 0);
  1481.     }
  1482.     }
  1483.  
  1484.   return q;
  1485. }
  1486.  
  1487. /* Pass a switch to the current accumulating command
  1488.    in the same form that we received it.
  1489.    SWITCHNUM identifies the switch; it is an index into
  1490.    the vector of switches gcc received, which is `switches'.
  1491.    This cannot fail since it never finishes a command line.  */
  1492.  
  1493. give_switch (switchnum)
  1494.      int switchnum;
  1495. {
  1496.   do_spec_1 (switches[switchnum].prefix, 1);
  1497.   do_spec_1 (switches[switchnum].part1, 1);
  1498.   do_spec_1 (" ", 0);
  1499.   if (switches[switchnum].part2 != 0)
  1500.     {
  1501.       do_spec_1 (switches[switchnum].part2, 1);
  1502.       do_spec_1 (" ", 0);
  1503.     }
  1504.   switches[switchnum].valid = 1;
  1505. }
  1506.  
  1507. /* Search for a file named NAME trying various prefixes including the
  1508.    user's -B prefix and some standard ones.
  1509.    Return the absolute pathname found.  If nothing is found, return NAME.  */
  1510.  
  1511. char *
  1512. find_file (name)
  1513.      char *name;
  1514. {
  1515.   int size;
  1516.   char *temp;
  1517.   int win = 0;
  1518.  
  1519.   /* Compute maximum size of NAME plus any prefix we will try.  */
  1520.  
  1521.   size = strlen (standard_exec_prefix);
  1522.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  1523.     size = strlen (user_exec_prefix);
  1524.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  1525.     size = strlen (env_exec_prefix);
  1526.   if (strlen (standard_exec_prefix) > size)
  1527.     size = strlen (standard_exec_prefix);
  1528.   if (strlen (standard_exec_prefix_1) > size)
  1529.     size = strlen (standard_exec_prefix_1);
  1530.   if (strlen (standard_startfile_prefix) > size)
  1531.     size = strlen (standard_startfile_prefix);
  1532.   if (strlen (standard_startfile_prefix_1) > size)
  1533.     size = strlen (standard_startfile_prefix_1);
  1534.   if (strlen (standard_startfile_prefix_2) > size)
  1535.     size = strlen (standard_startfile_prefix_2);
  1536.   if (machine_suffix)
  1537.     size += strlen (machine_suffix) + 1;
  1538.   size += strlen (name) + 1;
  1539.  
  1540.   temp = (char *) alloca (size);
  1541.  
  1542.   if (user_exec_prefix)
  1543.     {
  1544.       if (machine_suffix)
  1545.     {
  1546.       strcpy (temp, user_exec_prefix);
  1547.       strcat (temp, machine_suffix);
  1548.       strcat (temp, name);
  1549.       win = (access (temp, R_OK) == 0);
  1550.     }
  1551.       if (!win)
  1552.     {
  1553.       strcpy (temp, user_exec_prefix);
  1554.       strcat (temp, name);
  1555.       win = (access (temp, R_OK) == 0);
  1556.     }
  1557.     }
  1558.  
  1559.   if (!win && env_exec_prefix)
  1560.     {
  1561.       if (machine_suffix)
  1562.     {
  1563.       strcpy (temp, env_exec_prefix);
  1564.       strcat (temp, machine_suffix);
  1565.       strcat (temp, name);
  1566.       win = (access (temp, R_OK) == 0);
  1567.     }
  1568.       if (!win)
  1569.     {
  1570.       strcpy (temp, env_exec_prefix);
  1571.       strcat (temp, name);
  1572.       win = (access (temp, R_OK) == 0);
  1573.     }
  1574.     }
  1575.  
  1576.   if (!win)
  1577.     {
  1578.       if (machine_suffix)
  1579.     {
  1580.       strcpy (temp, standard_exec_prefix);
  1581.       strcat (temp, machine_suffix);
  1582.       strcat (temp, name);
  1583.       win = (access (temp, R_OK) == 0);
  1584.     }
  1585.       if (!win)
  1586.     {
  1587.       strcpy (temp, standard_exec_prefix);
  1588.       strcat (temp, name);
  1589.       win = (access (temp, R_OK) == 0);
  1590.     }
  1591.     }
  1592.  
  1593.   if (!win)
  1594.     {
  1595.       if (machine_suffix)
  1596.     {
  1597.       strcpy (temp, standard_exec_prefix_1);
  1598.       strcat (temp, machine_suffix);
  1599.       strcat (temp, name);
  1600.       win = (access (temp, R_OK) == 0);
  1601.     }
  1602.       if (!win)
  1603.     {
  1604.       strcpy (temp, standard_exec_prefix_1);
  1605.       strcat (temp, name);
  1606.       win = (access (temp, R_OK) == 0);
  1607.     }
  1608.     }
  1609.  
  1610.   if (!win)
  1611.     {
  1612.       if (machine_suffix)
  1613.     {
  1614.       strcpy (temp, standard_startfile_prefix);
  1615.       strcat (temp, machine_suffix);
  1616.       strcat (temp, name);
  1617.       win = (access (temp, R_OK) == 0);
  1618.     }
  1619.       if (!win)
  1620.     {
  1621.       strcpy (temp, standard_startfile_prefix);
  1622.       strcat (temp, name);
  1623.       win = (access (temp, R_OK) == 0);
  1624.     }
  1625.     }
  1626.  
  1627.   if (!win)
  1628.     {
  1629.       if (machine_suffix)
  1630.     {
  1631.       strcpy (temp, standard_startfile_prefix_1);
  1632.       strcat (temp, machine_suffix);
  1633.       strcat (temp, name);
  1634.       win = (access (temp, R_OK) == 0);
  1635.     }
  1636.       if (!win)
  1637.     {
  1638.       strcpy (temp, standard_startfile_prefix_1);
  1639.       strcat (temp, name);
  1640.       win = (access (temp, R_OK) == 0);
  1641.     }
  1642.     }
  1643.  
  1644.   if (!win)
  1645.     {
  1646.       if (machine_suffix)
  1647.     {
  1648.       strcpy (temp, standard_startfile_prefix_2);
  1649.       strcat (temp, machine_suffix);
  1650.       strcat (temp, name);
  1651.       win = (access (temp, R_OK) == 0);
  1652.     }
  1653.       if (!win)
  1654.     {
  1655.       strcpy (temp, standard_startfile_prefix_2);
  1656.       strcat (temp, name);
  1657.       win = (access (temp, R_OK) == 0);
  1658.     }
  1659.     }
  1660.  
  1661.   if (!win)
  1662.     {
  1663.       if (machine_suffix)
  1664.     {
  1665.       strcpy (temp, "./");
  1666.       strcat (temp, machine_suffix);
  1667.       strcat (temp, name);
  1668.       win = (access (temp, R_OK) == 0);
  1669.     }
  1670.       if (!win)
  1671.     {
  1672.       strcpy (temp, "./");
  1673.       strcat (temp, name);
  1674.       win = (access (temp, R_OK) == 0);
  1675.     }
  1676.     }
  1677.  
  1678.   if (win)
  1679.     return save_string (temp, strlen (temp));
  1680.   return name;
  1681. }
  1682.  
  1683. /* On fatal signals, delete all the temporary files.  */
  1684.  
  1685. void
  1686. fatal_error (signum)
  1687.      int signum;
  1688. {
  1689.   signal (signum, SIG_DFL);
  1690.   delete_failure_queue ();
  1691.   delete_temp_files ();
  1692.   /* Get the same signal again, this time not handled,
  1693.      so its normal effect occurs.  */
  1694.   kill (getpid (), signum);
  1695. }
  1696.  
  1697. int
  1698. main (argc, argv)
  1699.      int argc;
  1700.      char **argv;
  1701. {
  1702.   register int i;
  1703.   int value;
  1704.   int error_count = 0;
  1705.   int linker_was_run = 0;
  1706.   char *explicit_link_files;
  1707.  
  1708.   programname = argv[0];
  1709.  
  1710.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  1711.     signal (SIGINT, fatal_error);
  1712.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  1713.     signal (SIGHUP, fatal_error);
  1714.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  1715.     signal (SIGTERM, fatal_error);
  1716.  
  1717.   argbuf_length = 10;
  1718.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  1719.  
  1720.   obstack_init (&obstack);
  1721.  
  1722.   choose_temp_base ();
  1723.  
  1724.   /* Make a table of what switches there are (switches, n_switches).
  1725.      Make a table of specified input files (infiles, n_infiles).  */
  1726.  
  1727.   process_command (argc, argv);
  1728.  
  1729.   if (vflag)
  1730.     {
  1731.       extern char *version_string;
  1732.       fprintf (stderr, "g++ version %s\n", version_string);
  1733.       if (n_infiles == 0)
  1734.     exit (0);
  1735.     }
  1736.  
  1737.   if (n_infiles == 0)
  1738.     fatal ("No input files specified.");
  1739.  
  1740.   /* Make a place to record the compiler output file names
  1741.      that correspond to the input files.  */
  1742.  
  1743.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  1744.   bzero (outfiles, n_infiles * sizeof (char *));
  1745.  
  1746.   /* Record which files were specified explicitly as link input.  */
  1747.  
  1748.   explicit_link_files = (char *) xmalloc (n_infiles);
  1749.   bzero (explicit_link_files, n_infiles);
  1750.  
  1751.   for (i = 0; i < n_infiles; i++)
  1752.     {
  1753.       register struct compiler *cp;
  1754.       int this_file_error = 0;
  1755.  
  1756.       /* Tell do_spec what to substitute for %i.  */
  1757.  
  1758.       input_filename = infiles[i];
  1759.       input_filename_length = strlen (input_filename);
  1760.       input_file_number = i;
  1761.  
  1762.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  1763.  
  1764.       outfiles[i] = input_filename;
  1765.  
  1766.       /* Figure out which compiler from the file's suffix.  */
  1767.  
  1768.       for (cp = compilers; cp->spec; cp++)
  1769.     {
  1770.       if (strlen (cp->suffix) < input_filename_length
  1771.           && !strcmp (cp->suffix,
  1772.               infiles[i] + input_filename_length
  1773.               - strlen (cp->suffix)))
  1774.         {
  1775.           /* Ok, we found an applicable compiler.  Run its spec.  */
  1776.           /* First say how much of input_filename to substitute for %b  */
  1777.           register char *p;
  1778.  
  1779.           input_basename = input_filename;
  1780.           for (p = input_filename; *p; p++)
  1781.         if (*p == '/')
  1782.           input_basename = p + 1;
  1783.           basename_length = (input_filename_length - strlen (cp->suffix)
  1784.                  - (input_basename - input_filename));
  1785.           value = do_spec (cp->spec);
  1786.           if (value < 0)
  1787.         this_file_error = 1;
  1788.           break;
  1789.         }
  1790.     }
  1791.  
  1792.       /* If this file's name does not contain a recognized suffix,
  1793.      record it as explicit linker input.  */
  1794.  
  1795.       if (! cp->spec)
  1796.     explicit_link_files[i] = 1;
  1797.  
  1798.       /* Clear the delete-on-failure queue, deleting the files in it
  1799.      if this compilation failed.  */
  1800.  
  1801.       if (this_file_error)
  1802.     {
  1803.       delete_failure_queue ();
  1804.       error_count++;
  1805.     }
  1806.       /* If this compilation succeeded, don't delete those files later.  */
  1807.       clear_failure_queue ();
  1808.     }
  1809.  
  1810.   /* Run ld to link all the compiler output files.  */
  1811.  
  1812.   if (error_count == 0)
  1813.     {
  1814.       int tmp = execution_count;
  1815.       value = do_spec (link_spec);
  1816.       if (value < 0)
  1817.     error_count = 1;
  1818.       linker_was_run = (tmp != execution_count);
  1819.     }
  1820.  
  1821. #ifdef USE_COLLECT
  1822.   /* C++: now collect all the requirements for
  1823.      calling global constructors and destructors,
  1824.      and write them to a file.  We will link this file
  1825.      in when we run.  */
  1826.   if (linker_was_run && ! error_count)
  1827.     {
  1828.       int tmp = execution_count;
  1829.       value = do_spec (collect_spec);
  1830.       if (value < 0)
  1831.     error_count = 1;
  1832.       linker_was_run &= (tmp != execution_count);
  1833.     }
  1834. #endif
  1835.  
  1836.   /* If options said don't run linker,
  1837.      complain about input files to be given to the linker.  */
  1838.  
  1839.   if (! linker_was_run && error_count == 0)
  1840.     for (i = 0; i < n_infiles; i++)
  1841.       if (explicit_link_files[i])
  1842.     error ("%s: linker input file unused since linking not done",
  1843.            outfiles[i]);
  1844.  
  1845.   /* Set the `valid' bits for switches that match anything in any spec.  */
  1846.  
  1847.   validate_all_switches ();
  1848.  
  1849.   /* Warn about any switches that no pass was interested in.  */
  1850.   
  1851.   for (i = 0; i < n_switches; i++)
  1852.     if (! switches[i].valid)
  1853.       error ("unrecognized option `%s%s'",
  1854.          switches[i].prefix, switches[i].part1);
  1855.  
  1856.   /* Delete some or all of the temporary files we made.  */
  1857.  
  1858.   if (error_count)
  1859.     delete_failure_queue ();
  1860.   delete_temp_files ();
  1861.  
  1862.   exit (error_count);
  1863. }
  1864.  
  1865. xmalloc (size)
  1866.      int size;
  1867. {
  1868.   register int value = malloc (size);
  1869.   if (value == 0)
  1870.     fatal ("Virtual memory full.");
  1871.   return value;
  1872. }
  1873.  
  1874. xrealloc (ptr, size)
  1875.      int ptr, size;
  1876. {
  1877.   register int value = realloc (ptr, size);
  1878.   if (value == 0)
  1879.     fatal ("Virtual memory full.");
  1880.   return value;
  1881. }
  1882.  
  1883. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  1884.  
  1885. char *
  1886. concat (s1, s2, s3)
  1887.      char *s1, *s2, *s3;
  1888. {
  1889.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1890.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1891.  
  1892.   strcpy (result, s1);
  1893.   strcpy (result + len1, s2);
  1894.   strcpy (result + len1 + len2, s3);
  1895.   *(result + len1 + len2 + len3) = 0;
  1896.  
  1897.   return result;
  1898. }
  1899.  
  1900. char *
  1901. save_string (s, len)
  1902.      char *s;
  1903.      int len;
  1904. {
  1905.   register char *result = (char *) xmalloc (len + 1);
  1906.  
  1907.   bcopy (s, result, len);
  1908.   result[len] = 0;
  1909.   return result;
  1910. }
  1911.  
  1912. pfatal_with_name (name)
  1913.      char *name;
  1914. {
  1915.   extern int errno, sys_nerr;
  1916.   extern char *sys_errlist[];
  1917.   char *s;
  1918.  
  1919.   if (errno < sys_nerr)
  1920.     s = concat ("%s: ", sys_errlist[errno], "");
  1921.   else
  1922.     s = "cannot open %s";
  1923.   fatal (s, name);
  1924. }
  1925.  
  1926. perror_with_name (name)
  1927.      char *name;
  1928. {
  1929.   extern int errno, sys_nerr;
  1930.   extern char *sys_errlist[];
  1931.   char *s;
  1932.  
  1933.   if (errno < sys_nerr)
  1934.     s = concat ("%s: ", sys_errlist[errno], "");
  1935.   else
  1936.     s = "cannot open %s";
  1937.   error (s, name);
  1938. }
  1939.  
  1940. perror_exec (name)
  1941.      char *name;
  1942. {
  1943.   extern int errno, sys_nerr;
  1944.   extern char *sys_errlist[];
  1945.   char *s;
  1946.  
  1947.   if (errno < sys_nerr)
  1948.     s = concat ("installation problem, cannot exec %s: ",
  1949.         sys_errlist[errno], "");
  1950.   else
  1951.     s = "installation problem, cannot exec %s";
  1952.   error (s, name);
  1953. }
  1954.  
  1955. /* More 'friendly' abort that prints the line and file.
  1956.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  1957.  
  1958. void
  1959. fancy_abort ()
  1960. {
  1961.   fatal ("Internal gcc abort.");
  1962. }
  1963.  
  1964. #ifdef HAVE_VPRINTF
  1965.  
  1966. /* Output an error message and exit */
  1967.  
  1968. int 
  1969. fatal (va_alist)
  1970.      va_dcl
  1971. {
  1972.   va_list ap;
  1973.   char *format;
  1974.   
  1975.   va_start(ap);
  1976.   format = va_arg (ap, char *);
  1977.   vfprintf (stderr, format, ap);
  1978.   va_end (ap);
  1979.   fprintf (stderr, "\n");
  1980.   delete_temp_files (0);
  1981.   exit (1);
  1982. }  
  1983.  
  1984. error (va_alist) 
  1985.      va_dcl
  1986. {
  1987.   va_list ap;
  1988.   char *format;
  1989.  
  1990.   va_start(ap);
  1991.   format = va_arg (ap, char *);
  1992.   fprintf (stderr, "%s: ", programname);
  1993.   vfprintf (stderr, format, ap);
  1994.   va_end (ap);
  1995.  
  1996.   fprintf (stderr, "\n");
  1997. }
  1998.  
  1999. #else /* not HAVE_VPRINTF */
  2000.  
  2001. fatal (msg, arg1, arg2)
  2002.      char *msg, *arg1, *arg2;
  2003. {
  2004.   error (msg, arg1, arg2);
  2005.   delete_temp_files (0);
  2006.   exit (1);
  2007. }
  2008.  
  2009. error (msg, arg1, arg2)
  2010.      char *msg, *arg1, *arg2;
  2011. {
  2012.   fprintf (stderr, "%s: ", programname);
  2013.   fprintf (stderr, msg, arg1, arg2);
  2014.   fprintf (stderr, "\n");
  2015. }
  2016.  
  2017. #endif /* not HAVE_VPRINTF */
  2018.  
  2019.  
  2020. void
  2021. validate_all_switches ()
  2022. {
  2023.   struct compiler *comp;
  2024.   register char *p;
  2025.   register char c;
  2026.  
  2027.   for (comp = compilers; comp->spec; comp++)
  2028.     {
  2029.       p = comp->spec;
  2030.       while (c = *p++)
  2031.     if (c == '%' && *p == '{')
  2032.       /* We have a switch spec.  */
  2033.       validate_switches (p + 1);
  2034.     }
  2035.  
  2036.   p = link_spec;
  2037.   while (c = *p++)
  2038.     if (c == '%' && *p == '{')
  2039.       /* We have a switch spec.  */
  2040.       validate_switches (p + 1);
  2041.  
  2042.   /* Now notice switches mentioned in the machine-specific specs.  */
  2043.  
  2044. #ifdef ASM_SPEC
  2045.   p = ASM_SPEC;
  2046.   while (c = *p++)
  2047.     if (c == '%' && *p == '{')
  2048.       /* We have a switch spec.  */
  2049.       validate_switches (p + 1);
  2050. #endif
  2051.  
  2052. #ifdef CPP_SPEC
  2053.   p = CPP_SPEC;
  2054.   while (c = *p++)
  2055.     if (c == '%' && *p == '{')
  2056.       /* We have a switch spec.  */
  2057.       validate_switches (p + 1);
  2058. #endif
  2059.  
  2060. #ifdef SIGNED_CHAR_SPEC
  2061.   p = SIGNED_CHAR_SPEC;
  2062.   while (c = *p++)
  2063.     if (c == '%' && *p == '{')
  2064.       /* We have a switch spec.  */
  2065.       validate_switches (p + 1);
  2066. #endif
  2067.  
  2068. #ifdef CC1_SPEC
  2069.   p = CC1_SPEC;
  2070.   while (c = *p++)
  2071.     if (c == '%' && *p == '{')
  2072.       /* We have a switch spec.  */
  2073.       validate_switches (p + 1);
  2074. #endif
  2075.  
  2076. #ifdef LINK_SPEC
  2077.   p = LINK_SPEC;
  2078.   while (c = *p++)
  2079.     if (c == '%' && *p == '{')
  2080.       /* We have a switch spec.  */
  2081.       validate_switches (p + 1);
  2082. #endif
  2083.  
  2084. #ifdef LIB_SPEC
  2085.   p = LIB_SPEC;
  2086.   while (c = *p++)
  2087.     if (c == '%' && *p == '{')
  2088.       /* We have a switch spec.  */
  2089.       validate_switches (p + 1);
  2090. #endif
  2091.  
  2092. #ifdef STARTFILE_SPEC
  2093.   p = STARTFILE_SPEC;
  2094.   while (c = *p++)
  2095.     if (c == '%' && *p == '{')
  2096.       /* We have a switch spec.  */
  2097.       validate_switches (p + 1);
  2098. #endif
  2099. }
  2100.  
  2101. /* Look at the switch-name that comes after START
  2102.    and mark as valid all supplied switches that match it.  */
  2103.  
  2104. void
  2105. validate_switches (start)
  2106.      char *start;
  2107. {
  2108.   register char *p = start;
  2109.   char *filter;
  2110.   register int i;
  2111.  
  2112.   if (*p == '|')
  2113.     ++p;
  2114.  
  2115.   if (*p == '!')
  2116.     ++p;
  2117.  
  2118.   filter = p;
  2119.   while (*p != ':' && *p != '}') p++;
  2120.  
  2121.   if (p[-1] == '*')
  2122.     {
  2123.       /* Mark all matching switches as valid.  */
  2124.       --p;
  2125.       for (i = 0; i < n_switches; i++)
  2126.     if (!strncmp (switches[i].part1, filter, p - filter))
  2127.       switches[i].valid = 1;
  2128.     }
  2129.   else
  2130.     {
  2131.       /* Mark an exact matching switch as valid.  */
  2132.       for (i = 0; i < n_switches; i++)
  2133.     {
  2134.       if (!strncmp (switches[i].part1, filter, p - filter)
  2135.           && switches[i].part1[p - filter] == 0)
  2136.         switches[i].valid = 1;
  2137.     }
  2138.     }
  2139. }
  2140.